﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Aribaccio;
using BDPAutodetect.DataService;
using BDPAutodetect.Device;
using BDPAutodetect.Models;
using CommunityToolkit.Mvvm.ComponentModel;

namespace BDPAutodetect.Mgrcore
{
    /// <summary>
    /// 工位管理
    /// </summary>
    public partial class StationManager : BaseManager
    {
        /// <summary>
        /// 基础数据操作
        /// </summary>
        protected IDataServiceBasic DataService { get; set; }
        /// <summary>
        /// 工位数据集
        /// </summary>
        public List<MStation> Stations { get; protected set; }
        /// <summary>
        /// 是否有效存在工位信息
        /// </summary>
        public bool IsValid => this.Stations != null && this.Stations.Count > 1;

        /// <summary>
        /// 构造函数[注入]
        /// </summary>
        public StationManager(IDataServiceBasic argBusiness)
            : base("工位管理中心")
        {
            this.DataService = argBusiness;
            this.Stations = new List<MStation>() { new MStation() };
        }

        /// <summary>
        /// 初始化工位数据
        /// </summary>
        public override async Task Initialize()
        {
            if (this.IsValid) return;
            if (this.Stations == null) this.Stations = new List<MStation>();
            //获取数据库数据
            var resultDatas = this.DataService.SelectModelItemsTask<TBasicStation>();
            await resultDatas.ConfigureAwait(false);
            if (resultDatas == null || resultDatas.Result == null || !resultDatas.Result.ResultState || resultDatas.Result.Data == null) return;

            foreach (var stationItem in resultDatas.Result.Data.OrderBy(v => v.ID))
            {
                if (stationItem == null) continue;
                DeviceStationType stationType = stationItem.Code.ToEnum<DeviceStationType>();
                if (stationType == DeviceStationType.None) continue;
                this.Stations.Add(new MStation(stationItem.ID, stationItem.Code, stationItem.Name) { StationType = stationType });
            }
        }

        /// <summary>
        /// 更新工位数据
        /// </summary>
        /// <param name="argStation"></param>
        /// <returns></returns>
        public async Task<bool> UpdateStation(MStation argStation)
        {
            Task<Result> result = this.DataService.UpdateWhereTask<TBasicStation>(argStation.CloneTData(), v => v.ID == argStation.ID);
            await result.ConfigureAwait(false);
            if (result == null || result.Result == null || !result.Result.ResultState) return false;

            var station = this.Stations.First(v => v.ID == argStation.ID);
            if (station != null)
            {
                station.Code = argStation.Code;
                station.Name = argStation.Name;
                station.StationType = argStation.Code.ToEnum<DeviceStationType>();
            }
            return true;
        }

        /// <summary>
        /// 获取工位信息
        /// </summary>
        /// <param name="argStationID">工位ID</param>
        /// <returns></returns>
        public MStation? GetStation(int argStationID)
        {
            if (!this.IsValid) return default;
            return this.Stations.FirstOrDefault(v => v.ID == argStationID);
        }

        /// <summary>
        /// 获取工位信息
        /// </summary>
        /// <param name="argStationType">工位类型</param>
        /// <returns></returns>
        public MStation? GetStation(DeviceStationType argStationType)
        {
            if (!this.IsValid) return default;
            return this.Stations.FirstOrDefault(v => v.StationType == argStationType);
        }
    }
}
